package net.comm.util;

import java.text.MessageFormat;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.validator.Resources;

/**
 * 
 * 交易操作结果显示公用处理框架类
 * @author kingswx(wxsun@longtop.com)
 * @version 3.0
 * @date 2007-10-15 下午08:11:11
 *
 * TODO 支持如下几种交易处理结果显示
 * 		1.对话框
 * 		2.对话框+直接跳转
 * 		3.确认方式
 * 		4.多种选择方式
 * 		5.自定义客户端执行js脚本方式
 */
public class HtmlMessageProcess {

    private final static String beginScript = "<script>\n";
    private final static String endScript = "</script>\n";

    /**
     * 核心处理生成的html代码
     * @return
     */
    public static String generateHTML(HttpServletRequest request , 
    		HtmlMessageBox hmb)
    {
    	int alertType = hmb.getAlertType();
    	if(hmb.getPrintMessage()==null) {
        	hmb.setPrintMessage("");
        }

        //当操作结果不是直接跳转的方式
    	String msg = "";
        if(alertType != HtmlMessageBox.ALERTTYPE_DIRECT_GO 
        		&& alertType != HtmlMessageBox.ALERTTYPE_SCRIPT)
        {
	        if(alertType == HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD) {
	            msg = ExtStr.replace(hmb.getPrintMessage(), "\n", "\\n");
	        } else {
	            msg = ExtStr.replace(hmb.getPrintMessage(), "\n", "<br>");
	        }

	        msg = ExtStr.replace(msg, "\"", "\\\"");
	        if(hmb.isUseBeanMessage()) {
	            msg = Resources.getMessage(request, msg);
	        }
	        if (hmb.getFormatParams() != null && hmb.getFormatParams().length > 0) {
	            msg = MessageFormat.format(msg, hmb.getFormatParams());
	        }
        }

        /**
         * 根据请求不同方式生成不同的html代码
         */
        String forwardURL = hmb.getForwardUrl();
        String targetname = hmb.getTargetName().trim();
        
        StringBuffer outputHTML = new StringBuffer();
        switch (alertType) 
        {
        	//单一交易处理结果信息提示,直接使用js中的alert方式
	        case HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD:

	        	outputHTML.append("\talert(\"").append(msg);
	        	outputHTML.append("\");\n");
	        	
	        case HtmlMessageBox.ALERTTYPE_DIRECT_GO:
	        	
	            if(forwardURL != null && forwardURL.trim().length() > 0)
	            {
	                if(HtmlMessageBox.TARGETTYPE_BLANK
	                			.equalsIgnoreCase(targetname))
	                {
	                	outputHTML.append("open(\"");
	                	outputHTML.append(forwardURL);
	                	outputHTML.append("\");\n");
	                } 
	                
	                else if(HtmlMessageBox.TARGETTYPE_PARENT
	                			.equalsIgnoreCase(targetname))
	                {
	                    outputHTML.append("parent.location.href=\"");
	                	outputHTML.append(forwardURL);
	                	outputHTML.append("\";\n");
	                } 
	                
	                else if(HtmlMessageBox.TARGETTYPE_SELF
	                			.equalsIgnoreCase(targetname)) 
	                {
	                	outputHTML.append("location.href=\"");
	                	outputHTML.append(forwardURL);
	                	outputHTML.append("\";\n");
	                }
	                
	                else if(HtmlMessageBox.TARGETTYPE_TOP
	                			.equalsIgnoreCase(targetname)) 
	                {
	                	outputHTML.append("top.location.href=\"");
	                	outputHTML.append(forwardURL);
	                	outputHTML.append("\";\n");
	                }
	                
	                else if (targetname.length() > 0) 
	                {
	                	outputHTML.append("open(\"");
	                	outputHTML.append(forwardURL);
	                	outputHTML.append("\",\"");
	                	outputHTML.append(targetname);
	                	outputHTML.append("\");\n");
	                }
	            }
	            
	            outputHTML.insert(0 , beginScript);
	            outputHTML.append(endScript);
	            break;

	        	
	        case HtmlMessageBox.ALERTTYPE_CONFIRM:

	        	outputHTML.append(beginScript);
	        	outputHTML.append("if (confirm(\"");
	        	outputHTML.append(msg);
	        	outputHTML.append("\")) {\n");
	        	outputHTML.append(forwardURL);
	        	outputHTML.append("} else {");
	        	outputHTML.append(hmb.getElseurl());
	        	outputHTML.append("}");
	        	outputHTML.append(endScript);
        		break;

	        case HtmlMessageBox.ALERTTYPE_SCRIPT:
        		
	        	outputHTML.append(beginScript);
	        	outputHTML.append(forwardURL);
        		outputHTML.append(endScript);
        		break;
	        	
	        
	        //多种方式后续处理,并支持js中定时机制
	        case HtmlMessageBox.ALERTTYPE_PRINTTOHTML:

	            StringBuffer linkMsg = new StringBuffer();
	            StringBuffer lastMsg = new StringBuffer();
	            String defURL = null;
	            
	            if(forwardURL != null && forwardURL.trim().length() > 0) 
	            {
	            	String[] linkMsgs = null;
	                String[] urls = ExtStr.split(forwardURL.trim(), "|");
	                
	                String linkMsgStr = hmb.getLinkMsg();
	                if(linkMsgStr != null && linkMsgStr.trim().length() > 0) {
	                	linkMsgs = ExtStr.split(linkMsgStr.trim(), "|");
	                }
	                
	                if (linkMsgs != null && urls.length > 0
	                		&& linkMsgs.length == urls.length) 
	                {
	                    for (int i = 0; i < linkMsgs.length; i++) 
	                    {
	                        if (hmb.isUseBeanMessage()) 
	                        {
	                            String tmp = Resources.getMessage(request, linkMsgs[i]);
	                            if(tmp != null) {
	                            	linkMsgs[i] = tmp;
	                            }
	                        }

	                        linkMsg.append("<a target='");
	                        linkMsg.append(hmb.getTargetName());
	                        linkMsg.append("' href='");
	                        linkMsg.append(urls[i]);
	                        linkMsg.append("'>");
	                        linkMsg.append(linkMsgs[i]);
	                        linkMsg.append("</a><br>");
	                    }
	                }

	                if (linkMsgs == null || linkMsgs.length == 0) 
	                {
	                	defURL = urls[urls.length - 1];
	                	lastMsg.append("<a target='");
	                	lastMsg.append(hmb.getTargetName());
	                	lastMsg.append("' href=");
	                	lastMsg.append(defURL);
	                	lastMsg.append(">快速跳转,请点击这里</a>");
	                }
	            }

	            outputHTML.append(beginScript);
	            if (hmb.isEnabledTimer() && hmb.getForwardUrl().length() > 0) {
	                outputHTML.append("window.attachEvent(\"onload\",forwardUrl);\n");
	            }

	            outputHTML.append("window.attachEvent(\"onload\",init);\n");
	            outputHTML.append("function init(){");
	            outputHTML.append("var p=document.getElementById(\"message\");");
	            outputHTML.append("	\np.innerHTML=\"<span style='color:red'>");
	            outputHTML.append(hmb.getPrintMessage());
	            outputHTML.append("</span><br>");
	            outputHTML.append(linkMsg);
	            outputHTML.append(lastMsg);
	            outputHTML.append("\";}");

	            outputHTML.append("function ok(url){location.href=url}\n\n");
	            outputHTML.append("function forwardUrl(){");
	            outputHTML.append("oInter = window.setInterval(\"forward()\",1000);}");
	            outputHTML.append("var i=").append(hmb.getTimerForward());
	            outputHTML.append(";var oInter;");
	            outputHTML.append(";function forward(){if(i > 0){");
	            outputHTML.append("document.getElementById(\"timer\").innerHTML=i + \"秒自动返回\";");
	            outputHTML.append("i --;}else{self.location.href='");
	            outputHTML.append(defURL);
	            outputHTML.append("'");
	            outputHTML.append(";window.clearInterval(oInter);}}");
//	            outputHTML.append("i --;}else{windows.location.href='pen('");
//	            outputHTML.append(defURL);
//	            outputHTML.append("','");
//	            outputHTML.append(hmb.getTargetName());
//	            outputHTML.append("');window.clearInterval(oInter);}}");
	            outputHTML.append(endScript);
	            break;
	       
        }

        return outputHTML.toString();
    }

}

